<?php

/**
 * This is the model class for table "{{category_content}}".
 *
 * The followings are the available columns in table '{{category_content}}':
 * @property integer $id
 * @property string $name
 * @property integer $url_id
 */
class CategoryContent extends BaseModel
{
	public $route = 'content/index';
	public $get_param = 'category;page';
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return CategoryContent the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{category_content}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, template, alias', 'required'),
			array('name, template, alias', 'length', 'max'=>255),
			array('alias', 'match', 'pattern'=>'/^[a-z0-9_-]+$/', 'message'=>'Псевдоним должен состоять из "a-z", "0-9", "_", "-"'),
			array('onpage, url_id', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, description, template, alias, onpage', 'safe', 'on'=>'search'),
			array('id, name, description, template, alias, onpage, url_id', 'safe'	),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'url' => array(self::BELONGS_TO, 'Urls', 'url_id'),
			'items' => array(self::HAS_MANY, 'Content', 'category_id'),
			'lns' => array(self::HAS_MANY, 'CategoryContentLn', 'category_content_id'),
			'ln' => array(self::HAS_ONE, 'CategoryContentLn', 'category_content_id',
				'condition'=>'ln.ln=:ln',
				'params'=>array(':ln'=>Yii::app()->language),
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id'			=>	Yii::t('main', 'ID'),
			'name'			=>	Yii::t('main', 'Название'),
			'description'	=>	Yii::t('main', 'Описание'),
			'template'		=>	Yii::t('main', 'Шаблон'),
			'alias'			=>	Yii::t('main', 'Синоним'),
			'onpage'		=>	Yii::t('main', 'На странице елементов'),
			'url_id'		=>	Yii::t('main', 'Ссылка'),
		);
	}

	protected function beforeDelete()
	{
		if(parent::beforeDelete())
		{
			Content::model()->deleteAll(
				'category_id=:category_id',
				array(':category_id'=>$this->id)
			);
			Urls::model()->deleteAll(
				'id=:url_id',
				array(':url_id'=>$this->url_id)
			);
			return true;
		}
		else
			return false;
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('template',$this->template,true);
		$criteria->compare('alias',$this->alias,true);
		$criteria->compare('onpage',$this->onpage,true);
		$criteria->compare('url_id',$this->url_id);
		
		return new CActiveDataProvider($this, array(
			'pagination'=>array(
					'pageSize'=> Yii::app()->user->getState('pageSize', Yii::app()->params['defaultPageSize']),
			),
			'criteria'=>$criteria,
		));
	}
}